﻿Imports Neslab.FIM.FIMUtils
Imports Microsoft.ResourceManagement.Workflow.Activities
Imports Microsoft.ResourceManagement.WebServices.WSResourceManagement

Public Class SetEntitlements
    Inherits SequenceActivity

#Region "Declarations"

    Private debug As New Debugging
    Private userID As Guid
    Private displayName As String = Nothing
    Private primaryRole As String = Nothing
    Private entitlements As New List(Of String)
    Private _sqlLookup As Neslab.FIM.FIMUtils.sqlLookups

#End Region

#Region "Properties"

    Public Shared currentRequest_CurrentRequest1Property As System.Workflow.ComponentModel.DependencyProperty = DependencyProperty.Register("currentRequest_CurrentRequest1", GetType(Microsoft.ResourceManagement.WebServices.WSResourceManagement.RequestType), GetType(Neslab.FIM.SetEntitlements))
    <System.ComponentModel.DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)> _
    <System.ComponentModel.BrowsableAttribute(True)> _
    <System.ComponentModel.CategoryAttribute("Misc")> _
    Public Property currentRequest_CurrentRequest1 As Microsoft.ResourceManagement.WebServices.WSResourceManagement.RequestType
        Get
            Return CType(MyBase.GetValue(Neslab.FIM.SetEntitlements.currentRequest_CurrentRequest1Property), Microsoft.ResourceManagement.WebServices.WSResourceManagement.RequestType)
        End Get
        Set(value As Microsoft.ResourceManagement.WebServices.WSResourceManagement.RequestType)
            MyBase.SetValue(Neslab.FIM.SetEntitlements.currentRequest_CurrentRequest1Property, value)
        End Set
    End Property

#End Region

#Region "Code Activities"

    Private Sub prepareToReadUser_ExecuteCode(sender As Object, e As EventArgs)

        'Get the containing workflow
        Dim containingWorkflow As SequentialWorkflow = Nothing
        If Not SequentialWorkflow.TryGetContainingWorkflow(Me, containingWorkflow) Then
            Throw New InvalidOperationException("Make Membership: unable to get Containing Workflow")
        End If

        'Prepare the properties for the readInputRecord activity
        readUser.ActorId = WellKnownGuids.FIMServiceAccount
        userID = containingWorkflow.TargetId
        readUser.ResourceId = userID

        debug.Log("read user has user with ObjectID = " & containingWorkflow.TargetId.ToString)

    End Sub

    Private Sub readUserResult_ExecuteCode(sender As Object, e As EventArgs)

        displayName = readUser.Resource("DisplayName")
        primaryRole = readUser.Resource("primaryRole")

        debug.Log("User with DisplayName = " & displayName & " has User Role of " & primaryRole)

        'Get the entitlements
        _sqlLookup = New Neslab.FIM.FIMUtils.sqlLookups("Shadowverse")
        entitlements = _sqlLookup.getEntitlementsByRole(primaryRole)

    End Sub

    Private Sub prepareToUpdateUser_ExecuteCode(sender As Object, e As EventArgs)

        updateUser.ActorId = WellKnownGuids.FIMServiceAccount
        updateUser.ResourceId = userID

        Dim updateRequestParameters As New List(Of UpdateRequestParameter)

        For Each val As String In entitlements

            updateRequestParameters.Add(New UpdateRequestParameter("entitlements", UpdateMode.Modify, val))

        Next


        updateUser.UpdateParameters = updateRequestParameters.ToArray()

    End Sub

#End Region





End Class
